package com.css.fxfzfxqh.modules.buildingIndividual.repository.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzfxqh.constants.YNEnum;
import com.css.fxfzfxqh.modules.buildingIndividual.entity.BuildingIndividualDataVersion;
import com.css.fxfzfxqh.modules.buildingIndividual.param.PageQueryParam;
import com.css.fxfzfxqh.modules.buildingIndividual.repository.BuildingIndividualDataVersionRepository;
import com.css.fxfzfxqh.modules.buildingIndividual.vo.BuildingIndividualDataVersionVO;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @CreateTime: 2024/5/9 14:16
 * @Description:
 */
@Repository
public class BuildingIndividualDataVersionRepositoryImpl implements BuildingIndividualDataVersionRepository {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void insert(BuildingIndividualDataVersion buildingIndividualDataVersion) {
        String sql =
                "INSERT INTO fxqh_building_individual_data_version " +
                        "VALUES (:id, :dataRangeCountry, :dataRangeProvince, :dataRangeCity, :dataRangeArea, :dataName, " +
                        ":versionCode, :versionTime, :versionDesc, :attachmentIds, :uploadUser, :dataDesc, " +
                        ":uploadTime, :delFlag, :dataFileIds, :createUser, :createTime, :updateUser, :updateTime, :dataRange)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(buildingIndividualDataVersion);
        namedParameterJdbcTemplate.update(sql, source);

    }

    @Override
    public Map<String, Object> getPage(PageQueryParam queParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from fxqh_building_individual_data_version");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(queParams.getDataRangeCountry())) {
            whereSql.append(" and data_range_country = '" + queParams.getDataRangeCountry() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getDataRangeProvince())) {
            whereSql.append(" and data_range_province = '" + queParams.getDataRangeProvince() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getDataRangeCity())) {
            whereSql.append(" and data_range_city = '" + queParams.getDataRangeCity() + "'");
        }
        if (!PlatformObjectUtils.isEmpty(queParams.getDataRangeArea())) {
            whereSql.append(" and data_range_area = '" + queParams.getDataRangeArea() + "'");
        }




        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(BuildingIndividualDataVersionVO.class)).size();
        List<BuildingIndividualDataVersionVO> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(BuildingIndividualDataVersionVO.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void delete(String id) {
        String sql = "update fxqh_building_individual_data_version set del_flag= ? where id = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(),id);
    }
}
